package de.szut.bomberman.gui.options;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import de.szut.bomberman.gui.main.DefaultPanel;
import de.szut.bomberman.gui.main.GUIController;
import de.szut.bomberman.gui.mainmenu.MainMenuPanel;
import de.szut.bomberman.main.Controller;
import de.szut.gui.language.Languages;
import de.szut.gui.language.TranslatableComponent;
import de.szut.gui.language.TranslatableLabel;
import de.szut.gui.options.Options;
import de.szut.gui.options.OptionsComboBox;
import de.szut.gui.options.OptionsComponent;

public class OptionsPanel extends DefaultPanel {
	public static final int STARTPOSITIONX = 40;
	public static final int STARTPOSITIONY = 100;
	public static final int DIST = 26;
	public static final int BUTTONWIDTH = 190;
	public static final int BUTTONHEIGHT = 23;
	
	private static final long serialVersionUID = -727221251510455840L;
	
	private ArrayList<TranslatableComponent> translatableComponents;
	private ArrayList<OptionsComponent> optionsComponents;
	
	private TranslatableLabel lblLanguageInfo;
	private OptionsComboBox comboBox;
	
	public OptionsPanel() {
		translatableComponents = new ArrayList<TranslatableComponent>();
		optionsComponents = new ArrayList<OptionsComponent>();
		
		Languages.prepareComponent("options.OptionsPanel.header", getHeader(), translatableComponents);
		Languages.prepareComponent("options.OptionsPanel.return", getReturnButton(), translatableComponents);
		getReturnButton().addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseClicked(MouseEvent arg0) {
				Controller.getInstance().getOptions().saveOptions(optionsComponents);
				Controller.getInstance().getGuiController().getLanguages().setCurrentLanguage(Controller.getInstance().getOptions().getOptionValue(OptionKeys.LANGUAGE));
				Controller.getInstance().getGuiController().getLanguages().translateSpecificComponent(Controller.getInstance().getGuiController().getMainFrame());
				Controller.getInstance().getGuiController().switchPanel(new MainMenuPanel());
			}
		});
		
		int currentPosY = STARTPOSITIONY;
		
		lblLanguageInfo = new TranslatableLabel();
		Languages.prepareComponent("options.OptionPanel.language_info", lblLanguageInfo, translatableComponents);
		lblLanguageInfo.setBounds(STARTPOSITIONX, currentPosY, BUTTONWIDTH, BUTTONHEIGHT);
		lblLanguageInfo.setForeground(GUIController.COMPONENTFOREGROUNDCOLOR);
		lblLanguageInfo.setFont(GUIController.COMPONENTFONT);
		add(lblLanguageInfo);
		
		currentPosY += 0.8*DIST;
		
		String[] cmbbxValues = new String[Controller.getInstance().getGuiController().getLanguages().getLanguages().size()];
		int index = 0;
		for (String currentLanguage : Controller.getInstance().getGuiController().getLanguages().getLanguages()) {
			cmbbxValues[index] = currentLanguage;
			index ++;
		}
		
		comboBox = new OptionsComboBox(cmbbxValues);
		Options.prepareComponent(OptionKeys.LANGUAGE, comboBox, optionsComponents);
		comboBox.setBounds(STARTPOSITIONX, currentPosY, BUTTONWIDTH, BUTTONHEIGHT);
		comboBox.setForeground(GUIController.COMPONENTFOREGROUNDCOLOR);
		comboBox.setBackground(GUIController.COMPONENTBACKGROUNDCOLOR);
		comboBox.setFont(GUIController.COMPONENTFONT);
		add(comboBox);
		
		Controller.getInstance().getGuiController().getLanguages().translateListOfComponents(translatableComponents);
		Controller.getInstance().getOptions().loadOptions(optionsComponents);
		
	}

}
